package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.model.OrderType;
import com.luyuan.crm.sale.service.OrderService;
import com.ruijie.blueprint.spring.context.ApplicationContextHolder;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.action.WizardAccessor;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.foundation.security.AppUserContext;
import com.ruijie.util.StringUtils;

@Component("orderSaleAction")
@Scope("view")
public class OrderSaleAction extends BaseAction implements InitializingBean {

    private OrderStatus orderStatus;

    @Resource(name = "orderService")
    private OrderService orderService;

    private Order order = null;

    private String uuid;

    private String remarks;

    private Date startDate;

    private Date endDate;

    /**
     * 收到货
     */
    public void receivedOrder() {
        orderService.receivedOrder(order, remarks);
    }

    /**
     * 退货
     */
    public void rejectOrder() {
        orderService.rejectOrder(order, remarks);
    }

    public String startWizard(Order order) {
        WizardAccessor wizardAccessor = (WizardAccessor) ApplicationContextHolder.getApplicationContext().getBean(
                "wizardAccessor");

        setRequestAttribute("order", order, true);
        if (order.getOrderType() == OrderType.SINGLE_PRODUCT) {
            return wizardAccessor.start("singleOrderWizard", "update");
        }
        else {
            return wizardAccessor.start("orderWizard", "update");
        }
    }

    /**
     * 已发货的状态的列表
     * 
     * @return
     */
    public DataModel getOrderTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<org.hibernate.criterion.Order> orders = new ArrayList<org.hibernate.criterion.Order>();
        for (TableOrder tableOrder : tableOrders) {
            org.hibernate.criterion.Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Order.class);
        RestrictionTemplate r = new RestrictionTemplate(false);

        if (StringUtils.isNotEmpty(uuid)) {
            c.add(r.eq("id", uuid));
        }

        c.createCriteria("saleman", "s").add(r.eq("s.appUser", AppUserContext.getCurrentUser()));
        if (orderStatus != null) {
            c.add(r.eq("orderStatus", orderStatus));
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -3);
        Date sDate = calendar.getTime();
        Date eDate = new Date();
        if (startDate != null && startDate.after(sDate)) {
            sDate = startDate;
        }
        if (endDate != null && endDate.before(eDate)) {
            eDate = endDate;
        }

        c.add(r.between("createTime", sDate, eDate));
        c.addOrder(org.hibernate.criterion.Order.desc("createTime"));

        Page page = orderService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public boolean isNotEndStatus(Order order) {
        if (order == null) {
            return false;
        }
        return order.getOrderStatus() != OrderStatus.CANCELLED && order.getOrderStatus() != OrderStatus.RECEIVED;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String uuid = getRequestParameter("uuid");
        if (StringUtils.isNotEmpty(uuid)) {
            order = orderService.getById(uuid);
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public OrderStatus getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(OrderStatus orderStatus) {
        this.orderStatus = orderStatus;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}
